/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.common.interfaces.Worker;
import org.ourgrid.common.interfaces.WorkerExecutionServiceClient;
import org.ourgrid.common.interfaces.management.RemoteWorkerManagement;
import org.ourgrid.common.interfaces.management.WorkerManagement;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;
import org.ourgrid.worker.WorkerConstants;
import org.ourgrid.worker.controller.WorkerManagementClientFailureController;

@ReqTest(reqs="REQ004")
public class Req_004_Test extends WorkerAcceptanceTestCase {
	
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		workerAcceptanceUtil.simulateAuthentication();
	}
	
	/**
     *  Create a Worker with the public key property set to "publicKey1"
     *  Start the Worker with the public key "wrongPublicKey" - Verify if
     *  the following warn message was logged:
     *  o An unknown entity tried to start the Worker. Only the local modules 
     *  can perform this operation. Unknown entity public key: [wrongPublicKey].
	 */
	@ReqTest(test=" AT-004.1", reqs="REQ004")
	@Test public void test_at_004_1_WorkerInitializationWithWrongPublicKey() throws Exception {
		
		req_004_Util.startWorker("wrongPublicKey");
	} 
	
	/**
	 * Verifies if the remote objects were bound, after worker start.
     * 
     * Start a Worker with the correct public key;
     * 
     * Verify if the following message was logged:
     *     o "Worker has been successfully started."
     * 
     * Verify if the object "WORKER" (org.ourgrid.common.interfaces.Worker) is 
     * not bound
	 *
     * Get(lookup) the remote object "LOCAL_WORKER_MANAGEMENT" and verify if 
     * its type is:
     *     o org.ourgrid.common.interfaces.management.LocalWorkerManagement
     *
     * Verify if the object "REMOTE_WORKER_MANAGEMENT" 
     * (org.ourgrid.common.interfaces.management.RemoteWorkerManagement) is 
     * not bound
     * 
     * Get(lookup) the remote object "WORKER_STATUS" and verify if its type is:
     *     o org.ourgrid.common.interfaces.status.WorkerStatusProvider
     *
     * Verify if the object "WORKER_EXECUTION_LISTENER" 
     * (org.ourgrid.worker.executionservice.WorkerExecutionListener) is not 
     * bound
     * 
     * Get(lookup) the remote object "LOCAL_WORKER_MANAGEMENT_CLIENT_MONITOR" 
     * and verify if its type is:
     *     o org.ourgrid.jic.fd.jic.FailureInterestedEventProcessor
     *       
     * Verify if the ControlClient received the operation succeed message.
	 */
	@ReqTest(test=" AT-004.2", reqs="REQ004")
	@Test public void test_at_004_2_WorkerInitialization() throws Exception {
		WorkerComponent component = req_004_Util.startWorker();
		
		assertFalse(isBound(component, WorkerConstants.WORKER, Worker.class));
		assertTrue(isBound(component, WorkerConstants.LOCAL_WORKER_MANAGEMENT, 
				WorkerManagement.class));
		assertFalse(isBound(component, WorkerConstants.REMOTE_WORKER_MANAGEMENT, 
				RemoteWorkerManagement.class));
		assertFalse(isBound(component, WorkerConstants.WORKER_EXECUTION_SERVICE_OBJECT_NAME, 
				WorkerExecutionServiceClient.class));
		assertTrue(isBound(component, WorkerConstants.LOCAL_WORKER_MANAGEMENT_CLIENT_MONITOR, 
				WorkerManagementClientFailureController.class));
	}


}